package com.lsf.studentmanagerup.servlet.student;

import com.lsf.studentmanagerup.bean.Student;
import com.lsf.studentmanagerup.service.StudentService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @author 李树飞
 */
@WebServlet(name = "UpdateStudentServlet", urlPatterns = "/updateStudent")
public class UpdateStudentServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html; charset=UTF-8");

        //获取想客户端发送字符文本的对象
        PrintWriter writer = response.getWriter();

        //获取参数保存到student对象中
        Student student = new Student();
        student.setId(Integer.parseInt(request.getParameter("id")));
        student.setName(request.getParameter("name"));
        student.setGender(request.getParameter("gender"));
        student.setBirthday(request.getParameter("birthday"));
        student.setEmail(request.getParameter("email"));
        student.setComment(request.getParameter("comment"));
        student.setCid(Integer.parseInt(request.getParameter("cid")));

        //调用服务层修改的方法
        StudentService studentService = new StudentService();
        //输出true或false
        writer.print(studentService.updateStudentByIdService(student));
        writer.flush();
        writer.close();
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request,response);
    }
}
